package com.shoneworn.smartplug.View.activity;

import android.app.Activity;
import android.content.Intent;
import android.icu.text.DisplayContext;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ailin.shoneworn.mylibrary.NotifyManager;
import com.ailin.shoneworn.mylibrary.NotifyMsgEntity;
import com.shoneworn.smartplug.R;
import com.shoneworn.smartplug.View.service.TimerService;
import com.shoneworn.smartplug.data.CommandInfo;
import com.shoneworn.smartplug.data.DetailInfo;
import com.shoneworn.smartplug.data.DetailTextView;
import com.shoneworn.smartplug.data.DeviceBean;
import com.shoneworn.smartplug.interfaces.DetailTextviewInterface;
import com.shoneworn.smartplug.utils.CombineCommand;
import com.shoneworn.smartplug.utils.Constants;
import com.shoneworn.smartplug.utils.ReceiveCommand;
import com.shoneworn.smartplug.utils.SendCommand;
import com.shoneworn.smartplug.utils.UpdateAllData;

import java.lang.ref.WeakReference;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by admin on 2018/8/31.
 * <p>
 * 设备详情页：刚进入该页面，发送三条相应的指令。
 */

public class DetailActivity extends Activity implements Observer {

    private static DetailInfo detailInfo;

    private TextView mTvDevName;
    private TextView mTvFrequence;
    private TextView mTvTemperature;
    private TextView mTvCurrent;
    private TextView mTvVoltage;
    private TextView mTvPower;
    private TextView mTvName;
    private ImageView mIvBack;
    private ImageView mIvStatus;
    private DeviceBean bean;
    private ReceiveCommand receiveCommand;
    private CommandInfo commandInfo;
    private DetailTextView detailTextView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        new UpdateAllData();
        NotifyManager.getNotifyManager().addObserver(this);
        startService(new Intent(this, TimerService.class));
        Intent intent = getIntent();
        Bundle devBundle = intent.getBundleExtra("device");
        bean = (DeviceBean) devBundle.getSerializable("dev");
        detailInfo = new DetailInfo();
        commandInfo = new CommandInfo();
        receiveCommand = new ReceiveCommand();
        initView();

    }

    private void initView() {
        detailTextView = new DetailTextView();
        //Power
        mTvPower = (TextView) findViewById(R.id.tv_detail_power);
        mTvPower.setOnClickListener(new myClickListener());
        detailTextView.setmTvPower(mTvPower);

        //Current
        mTvCurrent = (TextView) findViewById(R.id.tv_detail_current);
        mTvCurrent.setOnClickListener(new myClickListener());
        detailTextView.setmTvCurrent(mTvCurrent);

        //Voltage
        mTvVoltage = (TextView) findViewById(R.id.tv_detail_voltage);
        mTvVoltage.setOnClickListener(new myClickListener());
        detailTextView.setmTvVoltage(mTvVoltage);

        //Frequence
        mTvFrequence = (TextView) findViewById(R.id.tv_detail_frequence);
        mTvFrequence.setOnClickListener(new myClickListener());
        detailTextView.setmTvFrequence(mTvFrequence);

        //Temperature
        mTvTemperature = (TextView) findViewById(R.id.tv_detail_temperature);
        mTvTemperature.setOnClickListener(new myClickListener());
        detailTextView.setmTvTemperature(mTvTemperature);

        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(new myClickListener());

        mTvDevName = (TextView) findViewById(R.id.tv_device_name);
        mTvDevName.setText(TextUtils.isEmpty(bean.getDevName()) ? "设备详情" : bean.getDevName());

//        setStatusImg(bean.getStatus());

    }

    class myClickListener implements View.OnClickListener {

        SendCommand sendCommand = new SendCommand();

        @Override
        public void onClick(View view) {
            sendCommand.init();

            switch (view.getId()) {
                case R.id.tv_detail_power:
                    //发送数据
                    sendCommand.sendReadV();
                    //接收数据
                    commandInfo = receiveCommand.recevieServer();
                    Toast.makeText(DetailActivity.this, "power正在刷新...", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.tv_detail_current:
                    sendCommand.sendReadI();
                    commandInfo = receiveCommand.recevieServer();
                    Toast.makeText(DetailActivity.this, "current正在刷新...", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.tv_detail_voltage:
                    sendCommand.sendReadF();
                    commandInfo = receiveCommand.recevieServer();
                    Toast.makeText(DetailActivity.this, "voltage正在刷新...", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.tv_detail_frequence:
                    sendCommand.sendReadT();
                    commandInfo = receiveCommand.recevieServer();
                    Toast.makeText(DetailActivity.this, "frequence正在刷新...", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.tv_detail_temperature:
                    sendCommand.sendReadPM();
                    commandInfo = receiveCommand.recevieServer();
                    Toast.makeText(DetailActivity.this, "temperature正在刷新...", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_back:
                    onStop();
                    finish();
                    break;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopService(new Intent(this, TimerService.class));
    }

    @Override
    public void update(Observable o, Object data) {
//        进行回调
//        UpdateAllData updateAllData = new UpdateAllData();
//        updateAllData.updateDetailData(detailTextView);
        if (data == null || !(data instanceof NotifyMsgEntity)) {
            return;
        }
        NotifyMsgEntity entity = (NotifyMsgEntity) data;

        int type = entity.getCode();

        if (Constants.NOTIFY_TO_DETAIL == type) {
            detailInfo = (DetailInfo) entity.getData();
            //更新详情页里面的数据
            updateDetailData(detailInfo);
        }
    }


    public void updateDetailData(DetailInfo detailInfo) {
        //只有数据不为空的情况下才对数据进行刷新
        if (!TextUtils.isEmpty(detailInfo.getmDetailPower())) {
            mTvPower.setText(detailInfo.getmDetailPower());
        }

        if (!TextUtils.isEmpty(detailInfo.getmDetailCurrent())) {
            mTvCurrent.setText(detailInfo.getmDetailCurrent());
        }

        if (!TextUtils.isEmpty(detailInfo.getmDetailVoltage())) {
            mTvVoltage.setText(detailInfo.getmDetailVoltage());
        }

        if (!TextUtils.isEmpty(detailInfo.getmDetailFrequence())) {
            mTvFrequence.setText(detailInfo.getmDetailFrequence());
        }

        if (!TextUtils.isEmpty(detailInfo.getmDetailTemperature())) {
            mTvTemperature.setText(detailInfo.getmDetailTemperature());
        }

        Toast.makeText(DetailActivity.this, "数据已刷新...", Toast.LENGTH_SHORT).show();
    }

    public void setStatusImg(int status) {
        switch (status) {
            case 0:
                mIvStatus.setImageResource(R.mipmap.item_dev_normal);
                break;
            case 1:
                mIvStatus.setImageResource(R.mipmap.item_dev_open);
                break;
            case 2:
                mIvStatus.setImageResource(R.mipmap.item_dev_error);
                break;
            default:
                mIvStatus.setImageResource(R.mipmap.item_dev_normal);
                break;
        }
    }
}
